import Vue from 'vue'
import router from './router'
import Cookies from 'js-cookie'
import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'
import './styles/element-variables.scss'

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'

import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log

import * as filters from './filters' // global filters

import adminSupport from 'lola-admin-support-el'
import WsClient from 'lola-ws-client'

import 'lola-admin-support-el/src/style/layout.scss'
import constants from './utils/constants.js'
import dutyUsers from './components/DutyUsers'
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
// if (process.env.NODE_ENV === 'production') {
//   const { mockXHR } = require('../mock')
//   mockXHR()
// }
Vue.prototype.$constants = constants
Vue.prototype.$https = adminSupport.lib.service

Vue.prototype.$lolaMixins = adminSupport.mixins
Vue.use(Element, {
  size: Cookies.get('size') || 'mini' // set element-ui default size
})
// 注入底层依赖
Vue.use(adminSupport)
Vue.component(dutyUsers.name, dutyUsers)
// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false

const loadingInstance = Element.Loading.service({
  fullscreen: true,
  lock: true,
  text: '系统载入中',
  background: 'rgba(0, 0, 0, 0.7)'
})

const reqConf = process.env.NODE_ENV === 'development'
  ? adminSupport.lib.appUtil.reqLocalConf(Vue)
  : adminSupport.lib.appUtil.reqConf(Vue)

// 载入json配置
reqConf.then(res => {
  console.log('应用初始化=====')
  // console.log(res)
  // 绑定页面自定义信息
  adminSupport.lib.appUtil.bindPage(res)
  // 绑定配置信息
  store.dispatch('settings/changeSetting', {
    key: 'conf',
    value: res
  })
  // 指定 当前系统编码
  store.dispatch('settings/changeSetting', {
    key: 'curSystemCode',
    value: 'm5000'
  })
  // 初始化全局ws客户端
  Vue.prototype.$socket = new WsClient(res.ws, (rsp) => {
    console.log('message')
  }, () => {
    console.log('ws connected')
  })

  setTimeout(() => {
    loadingInstance.close()
  }, 1000)

  // 载入wms系统配置
  const wmsConfUrl = Vue.prototype.$https.conf.api.get('wms').url + '/api/pub/conf'
  Vue.prototype.$https.get(wmsConfUrl).then(res => {
    // 绑定WMS配置信息
    store.dispatch('settings/changeSetting', {
      key: 'wmsConf',
      value: res.data
    })
    new Vue({
      el: '#app',
      router,
      store,
      render: h => h(App)
    })
  }).catch(e => {
    console.log(e)
    Element.Message.error('WMS配置获取失败...')
  })
}).catch(e => {
  // loadingInstance.close()
  console.log(e)
  Element.Message.error('配置获取失败...')
})

